package com.li.shanlin.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.li.shanlin.baseUtils.FacePlusUtils;
import com.li.shanlin.utils.BaseFileUtil;

/**
 * @author liyinlong
 *
 *         2017年3月9日下午12:59:25
 */
@Controller
@RequestMapping("/app")
public class AppController {
	////@RequestParam("image") String image,@RequestParam("shenfen") String shenfen,
	@RequestMapping("/picvscard")
	@ResponseBody
	public String getDetectPic(HttpServletRequest request,HttpSession session) {
		String image = request.getParameter("image");
		String shenfen = request.getParameter("shenfen");
		image = image.replaceAll(" ", "");
		shenfen = image.replaceAll(" ", "");
		String filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/image.jpg";
		String filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/shenfen.jpg";
		try {
			BaseFileUtil.decoderBase64File(image, filePath1);
			BaseFileUtil.decoderBase64File(shenfen, filePath2);
		} catch (Exception e) {
			e.printStackTrace();

		}
		File image1 = new File(filePath1);
		File shenfen1 = new File(filePath2);
		String result1 = FacePlusUtils.faceDetectMess(image1);
		String result2 = FacePlusUtils.faceOcridcard(shenfen1);
		//打印face_token
		JSONObject json1 = JSON.parseObject(result1);
		List<Map<String,Object>> json2 = (List<Map<String, Object>>) json1.get("faces");
		Map<String,Object> faces = json2.get(0);
		String face_token = (String) faces.get("token");		
		//打印身份信息
		Map<String,Object> json3 = JSON.parseObject(result2);
		String idcard_name = (String) json3.get("name");
		String idcard_number = (String) json3.get("id_card_number");
		String activeAndFacetoken = FacePlusUtils.faceVerifyActiveAndFacetoken(idcard_name, idcard_number, face_token);
		System.out.println("activeAndFacetoken:"+activeAndFacetoken);
		if(image1.exists()){
			image1.delete();
		}
		if(shenfen1.exists()){
			shenfen1.delete();
		}
		return activeAndFacetoken;
	}
	@RequestMapping("/card")
	@ResponseBody
	public String getDetectPic(@RequestParam("shenfen") String shenfen,HttpServletRequest request,HttpSession session) {
		String filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/shenfen.jpg";
		try {
			BaseFileUtil.decoderBase64File(shenfen, filePath2);
		} catch (Exception e) {
			e.printStackTrace();

		}
		File shenfen1 = new File(filePath2);
		String result2 = FacePlusUtils.faceOcridcard(shenfen1);
		if(shenfen1.exists()){
			shenfen1.delete();
		}
		return result2;
	}
	@RequestMapping("/getBase")
	@ResponseBody
	public String getDetectPic1(@RequestParam("image") MultipartFile image, HttpServletRequest request) {
		String filePath1 = null;
		if (!image.isEmpty()) {  
            try {  
                // 文件保存路径  
                filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"  
                        + image.getOriginalFilename();  
                // 转存文件  
                image.transferTo(new File(filePath1));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }
		
		String image1 = null;
		try {
			image1 = BaseFileUtil.encodeBase64File(filePath1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file1 = new File(filePath1);
		if(file1.exists()){
			file1.delete();
		}
		
		request.getSession().setAttribute("image", image1);
		return image1;
	}
}
